package com.briup.smartcity_group6.web.controller;

import com.briup.smartcity_group6.bean.basic.BaseConfig;
import com.briup.smartcity_group6.service.IBaseConfigService;
import com.briup.smartcity_group6.utils.Result;
import com.briup.smartcity_group6.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/auth/config")
public class BaseConfigController {
    @Autowired
    private IBaseConfigService service;
    @DeleteMapping
    public Result deleteById(@RequestParam(required = true) Integer id){
        service.deleteById(id);
        return ResultUtil.success("操作成功");
    }
    @GetMapping(value = "/yes")
    public BaseConfig getByYes() {
        return service.currentConfigYes();
    }
    @GetMapping(value = "/page")
    public Result getAllByPage(@RequestParam(required = true,defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = true,defaultValue = "3") Integer pageSize){

        return ResultUtil.success(service.findAllWithPage(pageNum,pageSize));
//        Result stoken=(Result) service.findAllWithPage(pageNum, pageSize);
//        return stoken
    }
    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result save(@RequestParam(required = true) String configIcon,
                     @RequestParam(required = true) String configInfo,
                     @RequestParam(required = true) String configName){

        BaseConfig baseConfig = new BaseConfig();
        baseConfig.setConfigIcon(configIcon);
        baseConfig.setConfigInfo(configInfo);
        baseConfig.setConfigName(configName);
        baseConfig.setConfigStatus(0);

        service.saveConfig(baseConfig);

        return ResultUtil.success("保存成功");
    }
    @PutMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result update(@RequestParam(required = true) String configIcon,
                       @RequestParam(required = true) Integer configId,
                       @RequestParam(required = true) String configInfo,
                       @RequestParam(required = true) String configName){

        BaseConfig baseConfig = new BaseConfig();
        baseConfig.setConfigIcon(configIcon);
        baseConfig.setConfigId(configId);
        baseConfig.setConfigInfo(configInfo);
        baseConfig.setConfigName(configName);

        service.updateConfig(baseConfig);
        return ResultUtil.success("更新成功");
    }
    @PutMapping(value = "status",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result updateStatus(@RequestParam(required = true) Integer id,
                             @RequestParam(required = true) Integer status){
        service.changeConfigStatus(id,status);
        return ResultUtil.success("更新状态成功");
    }

}
